<?php

class spFormLayout extends spForm
{
	private $table;
	private $layoutContainer;
		
	public function __construct( $data=null )
	{
		parent::__construct( $data );
		
		$title 	= isset($data["title"])?$data["title"]:"";
		$name 	= isset($data["name"])?$data["name"]:"";
		$action = isset($data["action"])?$data["action"]:"";
		$layoutType = isset($data["layout-type"])?$data["layout-type"]:"table";
		
		parent::setClass( "formContainer" );
		parent::addAttr( "name", $name );
		parent::addAttr( "action", $action );
		parent::addAttr( "method", "post" );
	
		if ( $layoutType=="table" )
		{
			$this->table = new spTable;
			$this->table->setClass( "CSSTableGenerator" );
			$this->table->addStyle( "table-layout", "fixed" );
			$this->table->addRowText( array($title) );
			parent::addChild( $this->table );
			
			$row = $this->table->addRow( array(0) );
			$this->layoutContainer = $row->findTag( "td" );
		}
		else
			$this->layoutContainer = $this;
		
		$this->processElements( $data );
		$this->processButtons( $data );
		$this->processHiddenFields( $data );
	}
	
	private function processHiddenFields( $data )
	{
		if ( !isset($data["hidden_fields"]) )
			return;
			
		foreach ( $data["hidden_fields"] as $field )
		{
			$this->layoutContainer->addChild( new spInputHidden( array( "name"=>$field["name"], "value"=>$field["value"] ) ) );
		}
	}
	
	private function processButtons( $data )
	{
		if ( !isset($data["buttons"]) )
			return;
		
		$buttons = new spButtonsHorizontal( array(
			"buttons"	=> $data["buttons"],
			"form_name"	=> $data["name"]
		));
		
		$this->layoutContainer->addChild( $buttons );
	}
	
	private function processElements( $data )
	{
		foreach ( $data["elements"] as $element )
		{
			if ( isset($element["show"]) && $element["show"]==false )
				continue;

			$show_field = isset($element["show_field"])?$element["show_field"]:true;

			$fieldset = null;
			
			if ( $show_field )
			{
				$fieldset = new spFieldSet;
				$fieldset->addStyle( "background-color", "#FFDEAD" );
				$fieldset->addStyle( "border", "1px solid #cccccc" );
				$fieldset->addStyle( "padding", "5px" );
				$this->layoutContainer->addChild( $fieldset );
				
				// title
				if ( isset( $element["title"] ) )
				{
					$legend = new spLegend;
					$h2 = new spHeading;
					$h2->setLevel( 2 );
					$legend->addChild( $h2 );
					$h2->setContent( $element["title"] );
					$fieldset->addChild( $legend );
				}
			}

			// element
			if ( isset( $element["element"] ) )
			{
				if ( $fieldset )
					$fieldset->addChild( $element["element"] );
				else
					$this->layoutContainer->addChild( $element["element"] );
			}

			// subtext
			if ( $show_field )
			{
				if ( isset( $element["subtext"] ) )
				{
					$fieldset->addChild( new spBreak );
					$i = new spItalic;
					$i->setContent( $element["subtext"] );
					$fieldset->addChild( $i );
				}
			}
		}
	}
}

?>